package io.renren.utils;


import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @program: jyindustry
 * @description:通用返回类
 * @author: zl
 * @create: 2020-07-09 18:05
 **/

public class RespDto<T> implements Serializable {

	private static final long serialVersionUID = 1L;

	private int code;
	private String msg;
	private T data;
	private int count;

	private static final String succ_msg = "";

	public static final int SUCC_CODE = 0;


	public RespDto() {

	}

	public RespDto(final T result) {
		this.code = SUCC_CODE;
		this.msg = succ_msg;
		this.data = result;
		if(result instanceof List) {
			this.count = ((List)result).size();
		}else {
			this.count = 1;
		}

	}

	public RespDto(final T result, final Map<String, String> header) {
		this.code = Constant.SUCCESS_CODE;
		this.msg = "success";
		this.data = result;
	}

	public RespDto(final int code, final String msg, final T result) {
		this.code = code;
		this.msg = msg;
		this.data = result;
	}

	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public T getData() {
		return data;
	}

	public void setData(T data) {
		this.data = data;
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public static <T> RespDto<T> succ() {
		final RespDto<T> dto = new RespDto<>();
		dto.setCode(Constant.SUCCESS_CODE);
		dto.setMsg(succ_msg);
		return dto;
	}

	public static <T> RespDto<T> succ(final String msg) {
		final RespDto<T> dto = succ();
		dto.setMsg(msg);
		return dto;
	}

	public RespDto(final T result, final int count) {
		this.code = SUCC_CODE;
		this.msg = succ_msg;
		this.data = (T)((PageReturnDto)result).getList();
		this.count=count;
	}


	//	public static <T> RespDto<T> succResult(final T result) {
	//		return new RespDto<>(result);
	//	}

	public static <T> RespDto<T> succResult(final T result, final Map<String, String> header) {
		return new RespDto<>(result, header);
	}

	public static <T> RespDto<T> succResult(final T result) {
		if(result instanceof PageReturnDto) {
			return new RespDto<>(result,new BigDecimal(((PageReturnDto)result).getTotalCount()).intValue());
		}else {
			return new RespDto<>(result);
		}
	}

	public static <T> RespDto<T> succ(final String msg, final T result) {
		final RespDto<T> dto = succ(msg);
		dto.setData(result);
		return dto;
	}

	public static <T> RespDto<T> fial(String msg) {
		final RespDto<T> dto = new RespDto<>();
		dto.setCode(500);
		dto.setMsg(msg);
		return dto;
	}

}
